package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.CategoryDao;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Autowired
    private CategoryBrandRelationService categoryBrandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTree() {
        List<CategoryEntity> categoryList =   baseMapper.selectList(null);
        List<CategoryEntity> subCategoryList = categoryList.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0 ).map( menu ->
                {
                    menu.setChildren(getSubCategoryList(menu,categoryList));
                    return menu;
                }
        ).sorted(Comparator.comparingInt(menu -> menu.getSort() == null ? 0 : menu.getSort())).collect(Collectors.toList());
        return subCategoryList;
    }

    @Override
    public List<Long> getCatelogPath(Long catelogId) {
        List<Long> catelogPath = new ArrayList<>();
        catelogPath.add(catelogId);
        CategoryEntity  parent = this.getById(catelogId);
        catelogPath.add(parent.getParentCid());
        if(parent.getParentCid() != 0 ){
            catelogPath.add(this.getById(parent.getParentCid()).getParentCid());
        };
        Collections.reverse(catelogPath);
        return catelogPath;
    }
    @Override
    @Transactional
    public void updateCategoryRelation(CategoryEntity category) {
        this.updateById(category);
        categoryBrandService.update(Wrappers.lambdaUpdate(CategoryBrandRelationEntity.class).set(CategoryBrandRelationEntity::getCatelogName,category.getName())
                .eq(CategoryBrandRelationEntity::getCatelogId,category.getCatId()));
    }

    @Override
    public List<CategoryEntity> getFirstLevel() {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_cid",0);
        return this.list(queryWrapper);
    }

    private List<CategoryEntity> getSubCategoryList(CategoryEntity subCategory, List<CategoryEntity> categoryList) {
        return  categoryList.stream().filter( category -> category.getParentCid().equals(subCategory.getCatId())).map( menu -> {
            menu.setChildren(getSubCategoryList(menu,categoryList));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> menu.getSort()==null?0:menu.getSort() )).collect(Collectors.toList());
    }

}